<template>
  <div>
    <div>
      <el-form :inline="true" style="margin: 20px">
        <el-form-item label="公司名称:">
          <el-input v-model="queryParam.companyName" placeholder="请输入公司名称模糊查询" clearable />
        </el-form-item>
        <el-button type="primary" @click="searchQuery">查询</el-button>
        <el-button @click="searchReset">重置</el-button>
      </el-form>
    </div>
    <div>
      <el-button
        type="primary"
        icon="el-icon-plus"
        style="position:relative;left:20px;top:-25px"
        @click="handleAdd"
      >新增</el-button>
    </div>
    <div class="table">
      <!-- table区域 -->
      <el-table
        :data="systemConfigList"
        tooltip-effect="dark"
        border
        style="width: 1250px;"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" align="center" width="120px" />
        <el-table-column prop="id" label="#" align="center" />
        <el-table-column prop="companyName" label="公司名称" />
        <el-table-column prop="companyContacts" label="联系人" />
        <el-table-column prop="companyAddress" label="公司地址" />
        <el-table-column prop="companyTel" label="公司电话" />
        <el-table-column prop="companyFax" label="公司传真" />
        <el-table-column prop="companyPostCode" label="公司邮编" />
        <el-table-column label="仓库权限" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.depotFlag === '0'" type="danger">关闭</el-tag>
            <el-tag v-else type="success">开启</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="客户权限" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.customerFlag === '0'" type="danger">关闭</el-tag>
            <el-tag v-else type="success">开启</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="负库存" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.minusStockFlag === '0'" type="danger">关闭</el-tag>
            <el-tag v-else type="success">开启</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="200px">
          <template slot-scope="scope">
            <el-button type="text" icon="el-icon-edit" @click="edit(scope.row)">编辑</el-button>
            <el-button
              type="text"
              icon="el-icon-delete"
              style="color: red;"
              @click="removeById(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        style="float: right"
        :current-page="current"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSize"
        class="page"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <div>
      <!-- Form -->
      <el-dialog :title.sync="dialogTitle" :visible.sync="dialogFormVisible">
        <el-form ref="form" :model="form" :rules="rules">
          <el-form-item label="公司名称:" :required="true" prop="companyName">
            <el-input v-model="form.companyName" placeholder="请输入公司名称" class="input" />
          </el-form-item>
          <el-form-item label="联系人:">
            <el-input v-model="form.companyContacts" placeholder="请输入联系人" class="input" />
          </el-form-item>
          <el-form-item label="公司地址:">
            <el-input v-model="form.companyAddress" placeholder="请输入公司地址" class="input" />
          </el-form-item>
          <el-form-item label="公司电话:">
            <el-input v-model="form.companyTel" placeholder="请输入公司电话" class="input" />
          </el-form-item>
          <el-form-item label="公司传真:">
            <el-input v-model="form.companyFax" placeholder="请输入公司传真" class="input" />
          </el-form-item>
          <el-form-item label="公司邮编:">
            <el-input v-model="form.companyPostCode" placeholder="请输入公司邮编" class="input" />
          </el-form-item>
          <!-- <el-form-item label="销售协议:">
                        <el-input v-model="form.saleAgreement" placeholder="请输入销售协议" class="input" />
                    </el-form-item> -->
          <el-form-item label="仓库权限:">
            <el-switch
              v-model="form.depotFlag"
              active-color="#13ce66"
              inactive-color="#DCDCDC"
              active-value="1"
              inactive-value="0"
            />（如果启用则需要到用户管理进行分配仓库）
          </el-form-item>
          <el-form-item label="客户权限:">
            <el-switch
              v-model="form.customerFlag"
              active-color="#13ce66"
              inactive-color="#DCDCDC"
              active-value="1"
              inactive-value="0"
            />（如果启用则需要到用户管理进行分配客户）
          </el-form-item>
          <el-form-item label="负库存:">
            <el-switch
              v-model="form.minusStockFlag"
              active-color="#13ce66"
              inactive-color="#DCDCDC"
              active-value="1"
              inactive-value="0"
            />（如果启用则单据支持负库存，批次商品除外）
          </el-form-item>
          <!-- <el-form-item label="以销定购:">
                        <el-switch v-model="form.purchaseBySaleFlag" active-color="#13ce66"
                            inactive-color="#DCDCDC" />（如果启用则根据销售订单来定制采购订单，进货后再发给客户）
                    </el-form-item> -->
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false; this.form = {};">取 消</el-button>
          <el-button type="primary" @click="submitForm('form')">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import systemConfigApi from '@/api/systemConfig'

export default {
  data() {
    return {
      form: {
        id: '',
        companyName: '',
        companyContacts: '',
        companyAddress: '',
        companyTel: '',
        companyFax: '',
        companyPostCode: '',
        salesAgreement: '',
        saleAgreement: '',
        depotFlag: 1,
        // purchaseBySaleFlag: false,
        customerFlag: 1,
        minusStockFlag: 1
      },
      checkParam: {
        id: '',
        companyName: ''
      },
      rules: {
        companyName: [{ required: true, message: '请输入公司名称', trigger: 'blur' }, { validator: this.checkIsNameExist, trigger: 'blur' }]
      },
      current: 1,
      pageSize: 5,
      total: 0,
      queryParam: { companyName: '' },
      systemConfigList: [],
      multipleSelection: [],
      dialogTitle: '新增',
      dialogFormVisible: false,
      companyName: '',
      companyContacts: '',
      companyAddress: '',
      companyTel: '',
      companyFax: '',
      companyPostCode: '',
      salesAgreement: '',
      saleAgreement: '',
      depotFlag: false,
      customerFlag: false,
      minusStockFlag: false.valueOf
    }
  },
  created() {
    this.list()
  },

  methods: {
    list() {
      systemConfigApi.list(this.current, this.pageSize, this.queryParam).then((res) => {
        this.systemConfigList = res.data.records
        this.pageSize = res.data.size
        this.current = res.data.current
        this.total = res.data.total
      })
    },
    checkIsNameExist(rule, value, callback) {
      this.checkParam.companyName = this.form.companyName
      this.checkParam.id = this.form.id
      systemConfigApi.checkIsNameExist(this.checkParam)
        .then(res => {
          if (res.status) {
            callback(new Error('名称已存在'))
          } else {
            callback()
          }
        })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.list()
    },
    handleCurrentChange(val) {
      this.current = val
      this.list()
    },
    edit(row) {
      this.dialogTitle = '修改'
      this.dialogFormVisible = true
      this.form = JSON.parse(JSON.stringify(row))
      // 清除自动触发的表单验证
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    handleAdd() {
      this.dialogFormVisible = true
      this.dialogTitle = '新增'
      this.form.id = undefined
      this.form = {}
      // 清除自动触发的表单验证
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    submitForm(form) {
      this.$refs[form].validate((valid) => { // 开启校验
        if (valid) { // 如果校验通过，请求接口，允许提交表单
          if (this.form.id === undefined) {
            systemConfigApi.add(this.form)
              .then(res => {
                this.$message({
                  type: 'success',
                  message: '添加成功'
                })
              })
            this.form = {}
            this.dialogFormVisible = false
            this.list()
          } else {
            systemConfigApi.update(this.form)
              .then(res => {
                this.$message({
                  type: 'success',
                  message: '修改成功'
                })
              })
          }
          this.form = {}
          this.dialogFormVisible = false
          this.list()
        }
      })
    },
    searchQuery() {
      this.list()
    },
    searchReset() {
      this.queryParam = {
        loginName: ''
      }
    },
    removeById(row) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        systemConfigApi.deleteById(row.id)
          .then(res => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          })
        this.list()
      })
    }
  }
}
</script>

<style>

</style>
